//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    indicatorDots:true,
    vertical:false,
    autoplay:true,
    interval:3000,
    duration:1200,
    topimg:[],
    city:'',
    topimg:'',
    serverlist:[],
    title:'',
    desc:'',
    clean:[],
    image:'',
    hot:'',
    recomlist:[]
  },
  bindViewTap:function(e){
    wx.navigateTo({
        url: '../city/index'
    })
  },
  gotoserver:function(e){
    wx.navigateTo({
      url: '../server/index'
    })
  },
  // onShow函数页面显示的时候调用，将数据接口放到此处可以实现页面的刷新
  onShow:function(){
    let that=this;
    // 调用getStorageAPI同步数据
    wx.getStorage({
      key: 'city',
      success:function(res){
          console.log(res.data);
          that.setData({
            city:res.data
          })
      }
    })
  },
  onLoad:function(){
    var that = this
    var openid = (wx.getStorageSync('openid'))
    if (!openid) {
      //调用微信登录接口  
      wx.login({
        success: function (loginCode) {
          if (loginCode.code) {
            wx.getUserInfo({
              withCredentials: true,
              success: function (res_user) {
                //console.log(res_user);
                wx.request({
                  url: app.globalData.serviceUrl + '/api/User/is_Register',
                  method: "POST",
                  dataType: "json",
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  data: { "code": loginCode.code, "userData": res_user.rawData },
                  success: function (res) {
                    //是否注册成功
                    if (res.data.code == 200) {
                      wx.setStorageSync('openid', res.data.data.openid);
                    } else {
                      console.log(res);
                    }
                  }
                })
              },
              fail: function () {//授权失败情况
                wx.showModal({
                  title: '警告通知',
                  content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
                  success: function (res) {
                    if (res.confirm) {
                      wx.openSetting({
                        success: (res) => {
                          if (res.authSetting['scope.userInfo']) {
                            wx.login({
                              success: function (res_Code) {
                                if (res_Code.code) {
                                  wx.getUserInfo({
                                    withCredentials: true,
                                    success: function (res_user) {
                                      //console.log(res_user);
                                      wx.request({
                                        url: app.globalData.serviceUrl + '/api/User/is_Register',
                                        method: "POST",
                                        dataType: "json",
                                        header: {
                                          'content-type': 'application/x-www-form-urlencoded'
                                        },
                                        data: { "code": res_Code.code, "userData": res_user.rawData },
                                        success: function (res) {
                                          //是否注册成功
                                          if (res.data.code == 200) {
                                            wx.setStorageSync('openid', res.data.data.openid);
                                          } else {
                                            console.log(res);
                                          }
                                        }
                                      })
                                    }
                                  })
                                }
                              }
                            })
                          }
                        }
                      })
                    }
                  }
                })
              }
            })
          }
        }
      })
    }
    wx.setNavigationBarTitle({
      title: "首页",
    })
    var page=this;
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function(res){
        // success
        var longitude=res.longitude;
        var latitude=res.latitude;
        page.loadCity(longitude,latitude);
    },
    fail: function() {
      // fail
    },
    complete: function() {
      // complete
    }
   })
  },
  loadCity:function(longitude,latitude){
    var page =this;
    
    wx.request({
      url: app.globalData.serviceUrl + '/api/User/getBanner',
      method:'GET',
      data:{},
      header:{
        'Accept':'application/json'
      },
      success:(res) => {
        console.log(res);
        this.setData({
          topimg:res.data.topimg,
          recomlist:res.data.recomlist
        })
      }
    })
  }
})
